package com.compass.mobile.ui.viewModel

import androidx.lifecycle.MutableLiveData
import com.compass.mobile.framework.utils.TipsToast
import com.compass.mobile.models.ArticleBean
import com.compass.mobile.models.ArticleBeanWarp
import com.compass.mobile.models.DepartmentBean
import com.compass.mobile.models.Doctor
import com.compass.mobile.models.DoctorHomePageWarp
import com.compass.mobile.models.DoctorInfo
import com.compass.mobile.models.TicketModelWarp
import com.compass.mobile.network.callback.IApiErrorCallback
import com.compass.mobile.network.manager.ApiManager
import com.compass.mobile.network.viewmodel.BaseViewModel
import com.google.gson.JsonObject

/**
 * Description： 医生相关
 * Date:2024/6/5 15:56
 */
class DoctorViewModel : BaseViewModel() {

    val doctorHomePageLiveData = MutableLiveData<DoctorHomePageWarp?>()
    val articleLiveData = MutableLiveData<MutableList<ArticleBean>?>()
    val followLiveData = MutableLiveData<String?>()
    val saveAppointmentLiveData = MutableLiveData<Any?>()//预约
    val ticketsLiveData = MutableLiveData<TicketModelWarp?>()
    val doctorsLiveData = MutableLiveData<MutableList<Doctor>?>()
    val departmentListLiveData = MutableLiveData<MutableList<DepartmentBean>?>()

    val doctorInfoLiveData = MutableLiveData<DoctorInfo?>() //医生数据

    fun getDoctorHomePage(doctorId: String?, patientId: String?, departmentId: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getDoctorHomePage(doctorId, patientId, departmentId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            doctorHomePageLiveData.value = it
        })
    }

    fun getTickets(pkSch: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getTickets(pkSch)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            ticketsLiveData.value=it

        })
    }


    fun saveAppointment(patientId:String?,pkSch: String?,param:String?) {

        //1 是Android
        val params = JsonObject().apply {
            addProperty("patientId", patientId)
            addProperty("pkSch", pkSch)
            addProperty("param", param)
            addProperty("payChannel","wx_tv")
        }
        launchUIWithResult(responseBlock = {
            ApiManager.api.saveAppointment(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            saveAppointmentLiveData.value=it

        })
    }


    /**
     * @param type 这里只查 视频
     */
    fun getArticleList(doctorId: String?, type: String, pageNo: Int, pageSize: Int = 4) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getArticleList(doctorId, type, pageNo, pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {

            if (it == null || it.list.isNullOrEmpty()) {
                articleLiveData.value = null
            } else {
                articleLiveData.value = it.list
            }

        })
    }


    /**
     * 关注/取消关注
     */
    fun follow(doctorId: String?, patientId: String?) {

        launchUIWithResult(responseBlock = {
            ApiManager.api.follow(doctorId, patientId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            followLiveData.value = it
        })
    }


    fun searchDoctors(keywords:String?,comeType:Int=0) {
        launchUIWithResult(responseBlock = {
            //1 是Android
            ApiManager.api.searchDoctors(keywords,comeType)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            doctorsLiveData.value = it
        })
    }

    fun searchDept(keywords: String?) {
        if (keywords.isNullOrEmpty()){
            return
        }
        launchUIWithResult(responseBlock = {
            ApiManager.api.geDeptList(keywords,null)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            departmentListLiveData.value = it
        })
    }




    /**
     * 查询医生数据
     */
    fun getDoctorInfo(doctorWyyx:String){
        launchUIWithResult(responseBlock = {
            ApiManager.api.getDoctorInfo(doctorWyyx)
        }, errorCall =object:IApiErrorCallback{
            override fun onError(code: Int?, error: String?) {
//                super.onError(code, error)
                TipsToast.showTips(error)
                doctorInfoLiveData.value = null
            }
        },
         successBlock = {
             if(it!=null){
                 doctorInfoLiveData.value  = it
             }
         })
    }
}